package org.keyroy.map_editor.ui.panels;

import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

import org.keyroy.map_editor.map.Map;
import org.keyroy.map_editor.map.MapResource;
import org.keyroy.map_editor.ui.component.ImageComponet;
import org.keyroy.map_editor.ui.layouts.GridFlowLayout;
import org.keyroy.map_editor.ui.listeners.SourceControlListener;

public abstract class SourcePanel extends JPanel {
	public static final Color COLOR_GLOBAL_RESOURCE = new Color(0x9999CC33, true);
	private static final long serialVersionUID = 1L;

	private int columns = 4;
	private boolean isAddDelete;
	private SourceControlListener controlListener;

	protected Map map;
	protected MapResource mapResource;

	public SourcePanel(int col, final Map map, SourceControlListener listener) {
		this.map = map;
		this.columns = col;
		controlListener = listener;
		mapResource = map.resource;
		mapResource.setSourceListener(new MapResource.SourceListener() {

			@Override
			public void onFloorImageAt(int ID, File file) {
				int size = getWidth() / columns;
				BufferedImage bufferedImage = getBufferedImage(ID);
				if (bufferedImage != null) {
					ImageComponet componet = new ImageComponet(ID, getBufferedImage(ID), size, size);
					componet.addMouseListener(getMouseListener(controlListener, componet.id));
					add(componet);
				}
			}

			@Override
			public void onFlushFloorImage() {
				reset();

			}

			@Override
			public void onBuildingImageAt(int ID, File file) {
				int size = getWidth() / columns;
				BufferedImage bufferedImage = getBufferedImage(ID);
				if (bufferedImage != null) {
					ImageComponet componet = new ImageComponet(ID, getBufferedImage(ID), size, size);
					componet.addMouseListener(getMouseListener(controlListener, componet.id));
					add(componet);
				}
			}

			@Override
			public void onFlushBuildingImage() {
				reset();
			}

		});

		setLayout(new GridFlowLayout(columns));

		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				if (isAddDelete == false) {
					isAddDelete = true;
					reset();
				}
			}
		});
	}

	public abstract Enumeration<Integer> getList();

	public abstract BufferedImage getBufferedImage(int id);

	public abstract MouseListener getMouseListener(SourceControlListener controlListener, int id);

	public void reset() {
		int size = getWidth() / columns;
		removeAll();
		try {
			InputStream inputStream = getClass().getResourceAsStream("/gnome_edit_delete.png");
			BufferedImage bufferedImage = ImageIO.read(inputStream);
			ImageComponet componet = new ImageComponet(0, bufferedImage, size, size);
			componet.addMouseListener(getMouseListener(controlListener, componet.id));
			add(componet);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Enumeration<Integer> keys = getList();
		while (keys.hasMoreElements()) {
			int key = keys.nextElement();
			BufferedImage bufferedImage = getBufferedImage(key);
			if (bufferedImage != null) {
				ImageComponet componet = new ImageComponet(key, getBufferedImage(key), size, size);
				componet.addMouseListener(getMouseListener(controlListener, componet.id));
				add(componet);
			}
		}
		try {
			getLayout().layoutContainer(SourcePanel.this);
			setPreferredSize(getLayout().preferredLayoutSize(SourcePanel.this));
			((JViewport) getParent()).setPreferredSize(getPreferredSize());
			((JScrollPane) ((JViewport) getParent()).getParent()).revalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		repaint();
	}

}
